home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / os2 / srefv12i.zip / userchk.rxx < prev    next >
Text File  |  1996-11-05  |  7KB  |  285 lines

  1. /* Check users/password module for SRE-FILTER. Uses contents of USERfile */
  2. /* Initializae with userfile,usequeue,usesem
  3.    call (via queues and semaphore) --  return q, return sem ,USERNAME PASSWORD
  4.    returns:  no match :  0
  5.                match:  N  priv_list
  6.         where N is the "# in list of match", and priv_list is space delimited
  7. */
  8.  
  9. parse upper arg userfile,  usequeue , USESEM , max_semwait,logon_limit
  10. call pmprintf(' SRE-FILTER Users Thread: Userfile='||userfile)
  11. call pmprintf(' SRE-FILTER Users Thread: queue='||usequeue)
  12. call pmprintf(' SRE-FILTER Users Thread: semaphore='||usesem)
  13. call pmprintf(' SRE-FILTER Users Thread: logon limit='||logon_limit)
  14.  
  15. mytid=dostid()
  16. call pmprintf(' SRE-FILTER Users thread: thread id='||mytid)
  17.  
  18. resetit:
  19. if  usequeue="" | USESEM="" then do
  20.    call pmprintf('SRE-FILTER Users thread: initialization ERROR: '||usequeue)
  21.    exit
  22. end
  23.  
  24. logfailb=1
  25. logfaile=0
  26.  
  27. call set_users(userfile)
  28.  
  29.  
  30. if nusers+nwilds=0 then do
  31.    call pmprintf(' SRE-FILTER Users thread: Warning: No valid entries in users file:'||userfile)
  32. end
  33. else do
  34.    call pmprintf(' SRE-FILTER Users thread:  #users='||nusers||', #wildcards= '||nwilds)
  35. end
  36.  
  37. /* Initialization now done == start waiting for requests for user info */
  38. signal on error name iserror
  39. signal on syntax name iserror
  40. bakme:
  41.  a=rxqueue('s',usequeue)
  42.  aq=queued()
  43.  if aq=0 then do
  44.     WOW=EVENTSEM_WAIT(USESEM,max_semwait)
  45.     aq=-1
  46.     if wow=640 then do
  47.         signal bakme
  48.     end
  49.     IF WOW<>0 THEN do         /* FATAL ERROR */
  50.         call pmprintf(' Fatal semaphore error in User thread ')
  51.         EXIT
  52.     end
  53.  end
  54.  wow=EVENTSEM_RESET(usesem)
  55.  if aq=-1 then
  56.    if queued()=0 then signal bakme
  57.  
  58.  pull isit0
  59. goobs:
  60.  isit0=translate(isit0,' ','000d0a09'x)
  61.  
  62.  if isit0=" " then signal bakme
  63.      parse var isit0  idnum ',' newq ',' newsem ',' ISIT
  64.      parse var idnum idnum host_nickname
  65.      if newq="" | newsem="" then do
  66.         call pmprintf(' SRE-FILTER Users Thread: missing queue or semaphore ')
  67.         signal bakme
  68.      end
  69.      newq=strip(newq); newsem=strip(newsem) ;ISIT=STRIP(ISIT)
  70.  
  71.  
  72.     if abbrev(strip(translate(isit)),'*DIE*') then 
  73.          exit
  74.  
  75.     if abbrev(strip(translate(isit)),'*RESET*') then do
  76.        parse var isit foo newfile .
  77.        if newfile<>' ' then userfile=newfile
  78.        call set_users(userfile)
  79.         call pmprintf('SRE-Filter Users Thread: Reread user file: 'userfile)
  80.    end
  81.    else do
  82.       dog1=fig_User(isit,host_nickname)
  83.       a=rxqueue('s',newq)
  84.       dog1=idnum','dog1
  85.       push  dog1
  86.       wow=eventsem_post(newsem)
  87.    end
  88.  
  89. signal bakme
  90.  
  91. serror:
  92. signal off error ; signal off syntax
  93. call pmprintf('SRE-Filter: error in Users thread ')
  94. a=rxqueue('d',usequeue)
  95. a=rxqueue('c',usequeue)
  96. a=eventsem_close(usesem)
  97. a=eventsem_create(usesem)
  98. a=rxqueue('s',newq)
  99. push idnum  ', 0 '
  100. wow=eventsem_post(newsem)
  101. call pmprintf('SRE-Filter: done resetting Users thread ')
  102. signal on error name iserror
  103. signal on syntax name iserror
  104. signal bakme
  105.  
  106. exit
  107.  
  108.  
  109. /* ---------- */
  110. fig_User:procedure expose names. pwds. orig. privs. wildorig. wildpwds. wildprivs. ,
  111.                 hostids. wildhosts. logfailb logfaile logfail. logon_limit
  112.   parse arg inline, host_nickname
  113.   host_nickname=upper(strip(host_nickname))
  114.   acum=0
  115.  
  116.  
  117.   inline=translate(inline,' ','000d0a09'x)
  118.   parse upper var inline username password who .
  119.  
  120.   who=strip(who)
  121.  
  122. /* got a request -- look for a match */
  123.  
  124.  
  125.  issec=time('s')
  126.  
  127. /* see if logon_limit has been exceeded for this ip address */
  128. if logon_limit>0 & logfailb <= logfaile & who<>' ' then do
  129.   if issec<logfail.logfaile.istime then do  /* reset at midnite */
  130.        drop logfail.
  131.        logfailb=0; logfaile=1
  132.   end
  133.   newb=0
  134.   do ikk=logfailb to logfaile
  135.       if logfail.ikk.istime < (issec-60) then do
  136.            newb=ikk ; iterate
  137.       end
  138.       if logfail.ikk.isip=who then acum=acum+1
  139.       if acum>logon_limit then leave
  140.   end
  141.   if newb>0 then do
  142.         do oo=logfailb to newb
  143.              drop logfail.oo.
  144.         end
  145.         logfailb=newb+1
  146.   end
  147.   if acum>logon_Limit then do
  148.       return -(acum+1)
  149.    end
  150. end
  151.  
  152. if username=' ' | password=' ' then do
  153.      return -acum
  154. end
  155.  
  156. oo=arraysearch('names','reslines',username,'SF')
  157. /* probably,there will be only 1 match. But in case there isn't,
  158.    check out several possible passwords */
  159.  ok=0
  160.  thematch=0
  161.  do mm=1 to oo
  162.      m1=reslines.mm
  163.      if hostids.m1<>host_nickname & hostids.m1<>"" then
  164.         iterate
  165.  
  166.      if m1=" " then iterate
  167.      if password=pwds.m1 then do   /* name and password match ! */
  168.         ok=m1
  169.         leave 
  170.      end
  171.      if pwds.m1='*' & ok=0 then do  /*note if it's a wild card match (first one only */
  172.         ok=-m1
  173.      end
  174.  end
  175. /* if ok<>0, then got a match. So return results */
  176.   if ok <> 0 then do     /* ok<0 means "wildcarded password */
  177.      ok=abs(ok)
  178.      thematch=orig.ok
  179.      theprivs=privs.ok
  180.    end
  181.  
  182. /* no exact match -- try wildcard list */
  183.   if ok=0 then do
  184.     do mm=1 to wildpwds.0
  185.  
  186.       if wildhosts.m1<>host_nickname & wildhosts.m1<>"" then
  187.          iterate
  188.  
  189.       if password=wildpwds.mm then do   /* name and password match ! */
  190.              theprivs=wildprivs.mm
  191.           thematch=wildorig.mm
  192.           leave
  193.       END
  194.      end
  195.   end
  196.  
  197. /*  crlf='0d0a'x*/
  198.   if thematch=0 then do
  199.      amess=-(acum+1)
  200.      if logon_limit>0 & who<>' ' then do
  201.         logfaile=logfaile+1
  202.         logfail.logfaile.isip=who
  203.         logfail.logfaile.istime=issec
  204.      end
  205.   end
  206.   else do
  207.      amess=thematch||' '||theprivs
  208.   end
  209.  
  210.   amess=translate(amess,' ','000d0a09'x)
  211.   return amess
  212.  
  213. /* ---------------- */
  214. /* read and set up users/pwds */
  215. set_users:
  216. parse arg afile
  217. nwilds=0 ; nusers=0 ; got1=0
  218. names.0=0 ; wildpwds.0=0
  219.  
  220. a=fileread(afile,'ulist',,'E')   /* read it to a stem variable */
  221. foo=do_extends(1)
  222.  
  223. if a=0 then do
  224.       call pmprintf(' SRE-FILTER Users thread: ERROR reading user-file: '||afile)
  225.       return 0
  226. end
  227.  
  228. ause2=arraysort('ulist',,,,,,'I')
  229. do mm=1 to ulist.0
  230.      t1=translate(strip(ulist.mm))
  231.      if t1=" " then iterate
  232.      if abbrev(t1,';')=1 then iterate
  233.      foo=upper(strip(word(t1,1)))
  234.      ahost=' '
  235.      if right(foo,2)='//' then do
  236.          ahost=left(foo,length(foo)-2)
  237.          parse upper var t1 ffo aname  apwd aprivs
  238.      end
  239.      else
  240.          parse upper var t1 aname  apwd aprivs
  241.  
  242.      got1=got1+1
  243.      if aname='*' then do
  244.            nwilds=nwilds+1
  245.            wildpwds.nwilds=strip(apwd) ; wildprivs.nwilds=strip(aprivs)
  246.            wildorig.nwilds=got1
  247.            wildhosts.nwilds=ahost
  248.      end
  249.      else do
  250.           nusers=nusers+1
  251.           names.nusers=strip(aname)
  252.           pwds.nusers=strip(apwd) ; privs.nusers=strip(aprivs)
  253.           orig.nusers=got1
  254.           hostids.nusers=ahost
  255.      end
  256.  end 
  257.  names.0=nusers 
  258.  wildpwds.0=nwilds
  259.  return 0
  260.  
  261. /************/
  262. /* Redo ulist, by treating lines starting with , as continuation lines */
  263. do_extends:procedure expose ulist.
  264.  
  265. if ulist.0=0 then return
  266. isnew=1
  267. tmps.1=ulist.1
  268. do mm=2 to ulist.0
  269.    ali=strip(ulist.mm)
  270.    if abbrev(ali,',')=0 then do
  271.         isnew=isnew+1
  272.         tmps.isnew=ulist.mm
  273.     end
  274.     else do
  275.         tmps.isnew=tmps.isnew||substr(ali,2)
  276.     end
  277. end
  278. do mm=1 to isnew
  279.     ulist.mm=tmps.mm
  280. end
  281. ulist.0=isnew
  282. return 0
  283.  
  284.  
  285.